package com.wang.web.servlet;


import com.alibaba.fastjson.JSON;
import com.wang.pojo.Brand;
import com.wang.pojo.PageBean;
import com.wang.service.BrandService;
import com.wang.service.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService brandService=new BrandServiceImpl();

     public void selectAll(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
         //调用service查询
         List<Brand> brands = brandService.selectAll();

         //转为JSON
         String jsonString = JSON.toJSONString(brands);

         //写数据
         resp.setContentType("text/json;charset=utf-8");
         resp.getWriter().write(jsonString);
     }


    public void add(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //接收品牌数据
        BufferedReader br=req.getReader();
        String params = br.readLine();

        //转为Brand对象
        Brand brand = JSON.parseObject(params, Brand.class);

        //调用service添加
        brandService.add(brand);

        //相应成功的标识
        resp.getWriter().write("success");


    }

    public void update(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
       //接受修改数据
        BufferedReader br=req.getReader();
        String params = br.readLine();

        //转为Brand对象
        Brand brand = JSON.parseObject(params, Brand.class);

        //调用service修改
        brandService.update(brand);

        //相应成功的标识
        resp.getWriter().write("success");

    }
    public void delete(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //接受修改数据
        BufferedReader br=req.getReader();
        String params = br.readLine();

        //转为Brand对象
        Brand brand = JSON.parseObject(params, Brand.class);
        Integer id = brand.getId();
        //System.out.println(id);

        //调用service修改
        brandService.delete(id);

        //相应成功的标识
        resp.getWriter().write("success");

    }

    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //接受修改数据
        BufferedReader br=req.getReader();
        String params = br.readLine();

        //转为int[]对象
        int[] ids = JSON.parseObject(params, int[].class);


        //调用service修改
        brandService.deleteByIds(ids);

        //相应成功的标识
        resp.getWriter().write("success");

    }
    public void selectByPage(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //接收当前页码和每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);



        //调用service查询
        PageBean<Brand> pageBean = brandService.selectByPage(currentPage, pageSize);



        //转为JSON
        String jsonString = JSON.toJSONString(pageBean);

        //写数据
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //接收当前页码和每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);


        //接受修改数据
        BufferedReader br=req.getReader();
        String params = br.readLine();

        //转为int[]对象
        Brand brand = JSON.parseObject(params, Brand.class);

        //调用service查询
        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);



        //转为JSON
        String jsonString = JSON.toJSONString(pageBean);

        //写数据
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }


}
